package com.gzbugu.bugusys.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.dao.IHQLQuery;
import com.gzbugu.bugusys.fields.DatabaseFields;
import com.gzbugu.bugusys.vo.Attendance;
import com.gzbugu.bugusys.vo.Staff;

/**
 *查询、统计记录数、统计金额、统计各考勤记录Service
 * 
 * @author chacha
 * 
 */

@Service
public class FindAttendanceService {
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "hqlQuery")
	private IHQLQuery hqlQuery;
	@Resource(name = "databaseFields")
	private DatabaseFields databaseFields;

	/**
	 *按员工编号查询
	 * 
	 * @param staffId
	 *            员工编号
	 * @param page
	 *            PageModel instance
	 * 
	 * @return 查询结果
	 */
	public List findById(String staffId, PageModel page) {
		String hql = "from Attendance attendance where attendance.staff.staffId ="
				+ staffId;
		return hqlQuery.find(hql, page);
	}

	/**
	 * 按时间区间查询
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @param page
	 *            PageModel instance
	 * 
	 * @return 查询结果
	 */
	public List findByTime(Date start, Date end, PageModel page) {
		String hql = "from Attendance attendance where attendance.start >='"
				+ new java.sql.Date(start.getTime())
				+ "' and attendance.start <='"
				+ new java.sql.Date(end.getTime())
				+ "' order by attendance.start desc";
		return hqlQuery.find(hql, page);
	}

	/**
	 * 按月份查询
	 * 
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * @param page
	 *            PageModel instance
	 * 
	 * @return 查询结果
	 */
	public List findByMonth(int year, int month, PageModel page) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();
		return this.findByTime(start, end, page);
	}

	/**
	 * 查询全部
	 * 
	 * @param page
	 *            PageModel instance
	 * 
	 * @return 查询结果
	 */
	public List findAll(PageModel page) {
		String hql = "from Attendance";
		return hqlQuery.find(hql, page);
	}

	/**
	 * 统计个人查询记录总数
	 * 
	 * @param staffId
	 *            员工编号
	 * 
	 * @return 记录数
	 */
	public int countById(String staffId) {
		String hql = "select count(*) from Attendance attendance where attendance.staff.staffId="
				+ staffId;
		List list = hqlQuery.find(hql);
		if (list != null) {
			Long sum = (Long) list.get(0);
			return sum.intValue();
		}
		return 0;
	}

	/**
	 * 统计按时间区间查询记录总数
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * 
	 * @return 记录数
	 */
	public int countByTime(Date start, Date end) {
		String hql = "select count(*) from Attendance attendance where attendance.start >='"
				+ new java.sql.Date(start.getTime())
				+ "' and attendance.start <='"
				+ new java.sql.Date(end.getTime()) + "'";
		List list = hqlQuery.find(hql);
		if (list != null) {
			Long sum = (Long) list.get(0);
			return sum.intValue();
		}
		return 0;
	}

	/**
	 * 统计按月份查询记录总数
	 * 
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * 
	 * @return 记录数
	 */
	public int countByMonth(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();
		return this.countByTime(start, end);
	}

	/**
	 * 统计记录总数
	 * 
	 * @return 总记录数
	 */
	public int countAll() {
		String hql = "select count(*) from Attendance";
		List list = hqlQuery.find(hql);
		if (list != null) {
			Long sum = (Long) list.get(0);
			return sum.intValue();
		}
		return 0;
	}

	/**
	 * 按员工编号统计各种考勤记录数
	 * 
	 * @param staffId
	 *            员工编号
	 * 
	 * @return 考勤统计集合
	 */
	public List countTypes(String staffId) {
		List result = new ArrayList();
		String hql = "select count(*) from Attendance a "
				+ "where a.staff.staffId =" + staffId + "and a.type.id=";
		for (int i = 18; i <= 21; i++) {
			List list = hqlQuery.find(hql + "'" + i + "'");
			if (list != null) {
				Long x = (Long) list.get(0);
				result.add(x.intValue());
			} else {
				result.add(0);
			}
		}
		List list = hqlQuery.find(hql + "'" + 4 + "'");
		if (list != null) {
			Long x = (Long) list.get(0);
			result.add(x.intValue());
		} else {
			result.add(0);
		}
		return result;
	}

	/**
	 * 按时间区间统计各种考勤记录数
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * 
	 * @return 考勤统计集合
	 */
	@SuppressWarnings("unchecked")
	public List countTypes(Date start, Date end) {
		List result = new ArrayList();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		String hql = "select count(*) from Attendance a where a.start >=:start and a.start <=:end and a.type.id =:id";
		paramsMap.put("start", start);
		paramsMap.put("end", end);
		for (int i = 18; i <= 21; i++) {
			paramsMap.put("id", (short) i);
			List list = hqlQuery.find(hql, paramsMap);
			if (list != null) {
				Long x = (Long) list.get(0);
				result.add(x.intValue());
			} else {
				result.add(0);
			}
		}
		paramsMap.put("id", (short) 4);
		List list = hqlQuery.find(hql, paramsMap);
		if (list != null) {
			Long x = (Long) list.get(0);
			result.add(x.intValue());
		} else {
			result.add(0);
		}
		return result;
	}

	/**
	 * 按月份统计各种考勤记录数
	 * 
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * 
	 * @return 考勤统计集合
	 */
	public List countTypes(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();
		return this.countTypes(start, end);
	}

	/**
	 * 统计全部各种考勤记录总数
	 * 
	 * @return 总考勤统计集合
	 */
	@SuppressWarnings("unchecked")
	public List countTypes() {
		List list = new ArrayList();
		Set late = databaseFields.getATTENDANCE_TYPE_LATE().getAttendances();
		Set leaveEarly = databaseFields.getATTENDANCE_TYPE_LEAVEEARLY()
				.getAttendances();
		Set leave = databaseFields.getATTENDANCE_TYPE_LEAVE().getAttendances();
		Set absenteeism = databaseFields.getATTENDANCE_TYPE_ABSENTEEISM()
				.getAttendances();
		Set others = databaseFields.getATTENDANCE_TYPE_ORTHERS()
				.getAttendances();
		list.add(late == null ? 0 : late.size());// 迟到
		list.add(leaveEarly == null ? 0 : leaveEarly.size());// 早退
		list.add(leave == null ? 0 : leave.size());// 请假
		list.add(absenteeism == null ? 0 : absenteeism.size());// 旷课
		list.add(others == null ? 0 : others.size());// 其他
		return list;
	}

	/**
	 * 按员工编号统计罚款金额
	 * 
	 * @param staffId
	 *            员工编号
	 * 
	 * @return 罚款金额
	 */
	public int countPay(String staffId) {
		String hql = "select sum(attendance.payment) from Attendance attendance where attendance.staff.staffId ="
				+ staffId;
		List list = hqlQuery.find(hql);
		if (list != null) {
			Long sum = (Long) list.get(0);
			return sum.intValue();
		}
		return 0;
	}

	/**
	 * 按时间区间统计罚款金额
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * 
	 * @return 罚款金额
	 */
	public int countPay(Date start, Date end) {
		String hql = "select sum(attendance.payment) from Attendance attendance "
				+ "where attendance.start >='"
				+ new java.sql.Date(start.getTime())
				+ "' and attendance.start <='"
				+ new java.sql.Date(end.getTime()) + "'";
		List list = hqlQuery.find(hql);
		if (list != null && !list.isEmpty()) {
			Long sum = (Long) list.get(0);
			return sum == null ? 0 : sum.intValue();
		}
		return 0;
	}

	/**
	 * 按月份统计罚款金额
	 * 
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * 
	 * @return
	 */
	public int countPay(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();
		return this.countPay(start, end);
	}

	/**
	 * 统计全部罚款金额
	 * 
	 * @return 总罚款金额
	 */
	public int countPay() {
		String hql = "select sum(attendance.payment) from Attendance attendance";
		List list = hqlQuery.find(hql);
		if (list != null) {
			Long sum = (Long) list.get(0);
			if (sum == null)
				return 0;
			return sum.intValue();
		}
		return 0;
	}

}
